package com.wwwarehouse.taskcenter.fragment.warehouse_reg;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSONObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wwwarehouse.common.BuildConfig;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.activity.media.SelectImageActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.bean.upload.FileUploadBean;
import com.wwwarehouse.common.common.Common;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.custom_widget.time_pick.ChoosePickerDialog;
import com.wwwarehouse.common.custom_widget.time_pick.DateAndTimePickerDialog;
import com.wwwarehouse.common.custom_widget.time_pick.DateUtil;
import com.wwwarehouse.common.custom_widget.time_pick.KeyBoardEditText;
import com.wwwarehouse.common.eventbus_event.BluetoothScanResultEvent;
import com.wwwarehouse.common.http.NetUtils;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.FileUtils;
import com.wwwarehouse.common.tools.PermissionUtils;
import com.wwwarehouse.common.tools.ThreadUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.BillBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.yolanda.nohttp.Binary;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static android.app.Activity.RESULT_OK;
import static com.wwwarehouse.common.activity.media.SelectImageActivity.KEY_SELECT_IMAGE_PATH;


/**
 * Created by clannad.shao on 2017/11/16.
 *
 * @author clannad_sk
 * @date 2017/11/16
 * 入库登记fragment
 */
@Route(path = TaskCenterConstant.whouseRegStep)
public class WhouseRegFirstFragment extends BaseFragment implements View.OnClickListener,
        PermissionUtils.PermissionCallbacks, TextWatcher {

    private RelativeLayout idWareRegTimeRl;
    private ImageView idWareRegTimeIv;
    private TextView idWareRegTimeTv;
    private RelativeLayout idWareRegTypeRl;
    private ImageView idWareRegTypeIv;
    private TextView idWareRegTypeTv;
    private KeyBoardEditText idWareRegEt;
    private RelativeLayout idWareRl1;
    private ImageView idWareUploadIv1;
    private ImageView idWareRegImg1;
    private ProgressBar idWarePro1;
    private RelativeLayout idWareRl2;
    private ImageView idWareUploadIv2;
    private ImageView idWareRegImg2;
    private ProgressBar idWarePro2;
    private RelativeLayout idWareRl3;
    private ImageView idWareUploadIv3;
    private ImageView idWareRegImg3;
    private ProgressBar idWarePro3;
    private TextView idWareRegNextTv;
    private EasyPopupWindow mEasyPopupWindow;
    private FileUtils mFileUtils;
    private File mFile;
    private static final int REQUEST_UPLOAD_IMAGE_CAMERA = 0;
    private static final int POSITION_ONE = 1;
    private static final int POSITION_TWO = 2;
    private static final int POSITION_THREE = 3;
    private static final int POSITION_DATE_TIME = 4;
    private static final int POSITION_UNIT = 5;
    private static final int POSITION_NUM = 6;
    private static final int REQUEST_UNIT_CODE = 20;
    private static final int REQUEST_UNIT_TWICE_CODE = 21;
    private static final int GALLERY = 99;
    private static final int PROGRESS_COMPLETE = 100;
    private static final String REQUEST_SUCCESS_CODE = "0";

    private String path;
    /**
     * 当前item
     */
    private int currentItem;
    /**
     * 上传图片源文件  注意clear以及remove
     */
    private Map<Integer, String> fileMap;
    /**
     * 支持多图上传
     */
    private List<Binary> binaries;
    /**
     * 对应位置的binary，用于主动取消
     */
    private Map<Integer, Binary> binaryMap;
    /**
     * 某个位置是否主动取消
     */
    private Map<Integer, Boolean> posIsActCancel;
    /**
     * dataCollection
     */
    private Map<Object, Object> contentMap;
    /**
     * 是否已准备好数据(实际用于控制线程)
     */
    private boolean isReady = false;
    private Runnable scanRunnable;
    /**
     * 数据集是否已准备完成
     */
    private boolean isComplete = false;
    private List<BillBean> billBeanList;
    private List<String> unitContent;
    private Map<String, Object> getUnitMap;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.task_center_warehouse_reg_firststep, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //初始化view
        initView();
    }

    private void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        isReady = false;
        idWareRegTimeRl = $(R.id.idWareRegTimeRl);
        idWareRegTimeIv = $(R.id.idWareRegTimeIv);
        idWareRegTimeTv = $(R.id.idWareRegTimeTv);
        idWareRegTypeRl = $(R.id.idWareRegTypeRl);
        idWareRegTypeIv = $(R.id.idWareRegTypeIv);
        idWareRegTypeTv = $(R.id.idWareRegTypeTv);
        idWareRegEt = $(R.id.idWareRegEt);
        idWareRl1 = $(R.id.idWareRl1);
        idWareUploadIv1 = $(R.id.idWareUploadIv1);
        idWareRegImg1 = $(R.id.idWareRegImg1);
        idWarePro1 = $(R.id.idWarePro1);
        idWareRl2 = $(R.id.idWareRl2);
        idWareUploadIv2 = $(R.id.idWareUploadIv2);
        idWareRegImg2 = $(R.id.idWareRegImg2);
        idWarePro2 = $(R.id.idWarePro2);
        idWareRl3 = $(R.id.idWareRl3);
        idWareUploadIv3 = $(R.id.idWareUploadIv3);
        idWareRegImg3 = $(R.id.idWareRegImg3);
        idWarePro3 = $(R.id.idWarePro3);
        idWareRegNextTv = $(R.id.idWareRegNextTv);
        idWareRegTimeRl.setOnClickListener(this);
        idWareRegTypeRl.setOnClickListener(this);
        idWareRegNextTv.setOnClickListener(this);
        idWareRl1.setOnClickListener(this);
        idWareRl2.setOnClickListener(this);
        idWareRl3.setOnClickListener(this);
        idWareRegNextTv.setClickable(false);
        unitContent = new ArrayList<>();
        mFileUtils = new FileUtils();
        fileMap = new LinkedHashMap<>(3 * 2);
        binaryMap = new HashMap<>(3 * 2);
        posIsActCancel = new HashMap<>(3 * 2);
        getUnitMap = new HashMap<>(2 * 2);
        contentMap = new HashMap<>();
        scanRunnable = new ScanRunnable();
        ThreadUtils.newThread(scanRunnable);
        getUnitMap.put("codeType", "STORAGE_BILL_TYPE");
        requestData(REQUEST_UNIT_CODE, TaskCenterConstant.GET_UNIT, getUnitMap);
        idWareRegEt.addTextChangedListener(this);
    }

    private void requestData(int what, String url, Map<String, Object> map) {
        if (!NetUtils.isHttpConnected(mActivity)) {
            toast(R.string.warehouse_reg_no_network);
            return;
        }
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int what) {
                if (REQUEST_UNIT_CODE == what) {
                    //进入页面获取单位请求
                    if (REQUEST_SUCCESS_CODE.equals(commonClass.getCode())) {
                        //成功
                        billBeanList = JSONObject.parseArray(commonClass.getData().toString(), BillBean.class);
                        if (billBeanList == null || billBeanList.isEmpty()) {
                            return;
                        }
                        for (BillBean billBean : billBeanList) {
                            unitContent.add(StringUtils.isNullString(billBean.getName()) ? "" : billBean.getName());
                        }
                    } else {
                        //第一次获取单位badRequest静默处理
                    }
                } else if (REQUEST_UNIT_TWICE_CODE == what) {
                    //第二次获取单位
                    if (REQUEST_SUCCESS_CODE.equals(commonClass.getCode())) {
                        billBeanList = JSONObject.parseArray(commonClass.getData().toString(), BillBean.class);
                        if (billBeanList == null || billBeanList.isEmpty()) {
                            toast(getString(R.string.warehouse_reg_getunit_error));
                            return;
                        }
                        for (BillBean billBean : billBeanList) {
                            unitContent.add(StringUtils.isNullString(billBean.getName()) ? "" : billBean.getName());
                        }
                        showPickUnitDialog();
                    } else {
                        //badRequest
                        toast(getString(R.string.warehouse_reg_getunit_error));
                    }
                } else {
                    //otherRequest
                }

            }

            @Override
            public void onFailed(String s, int what) {
                if (REQUEST_UNIT_TWICE_CODE == what) {
                    //第二次获取单位
                    toast(getString(R.string.warehouse_reg_getunit_error));
                } else {
                    //other
                }
            }

            @Override
            public void onFinish(int i) {

            }
        }, what);
    }


    @Override
    public void setTitle() {
        if (peekFragment() instanceof WhouseRegFirstFragment) {
            mActivity.setTitle(getString(R.string.warehouse_reg_title));
        }
    }


    @Override
    public void onClick(View view) {
        if (Common.getInstance().isNotFastClick()) {
            if (view.getId() == R.id.idWareRegTimeRl) {
                showPickDateAndTime();
            } else if (view.getId() == R.id.idWareRegTypeRl) {
                hideInput(view);
                showPickUnitDialog();
            } else if (view.getId() == R.id.idWareRl1) {
                showPop(POSITION_ONE);
            } else if (view.getId() == R.id.idWareRl2) {
                showPop(POSITION_TWO);
            } else if (view.getId() == R.id.idWareRl3) {
                showPop(POSITION_THREE);
            } else if (view.getId() == R.id.btn_take_photo) {
                PermissionUtils.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                hidePop();
            } else if (view.getId() == R.id.btn_gallery) {
                startActivityForResult(new Intent(getActivity(), SelectImageActivity.class), GALLERY);
                hidePop();
            } else if (view.getId() == R.id.btn_cancel) {
                hidePop();
            } else if (view.getId() == R.id.idWareRegNextTv) {
                //next
                Fragment fragment = new WhouseRegSecFragment();
                Bundle bundle = new Bundle();
                bundle.putSerializable(getString(R.string.whouse_reg_img1), (FileUploadBean.DataBean)contentMap.get(POSITION_ONE));
                bundle.putSerializable(getString(R.string.whouse_reg_img2), (FileUploadBean.DataBean)contentMap.get(POSITION_TWO));
                bundle.putSerializable(getString(R.string.whouse_reg_img3), (FileUploadBean.DataBean)contentMap.get(POSITION_THREE));
                bundle.putParcelable(getString(R.string.whouse_reg_billUnit), (BillBean) contentMap.get(POSITION_UNIT));
                bundle.putString(getString(R.string.whouse_reg_dateTime), contentMap.get(POSITION_DATE_TIME) + ":00");
                bundle.putString(getString(R.string.whouse_reg_billNum), (String) contentMap.get(POSITION_NUM));
                bundle.putString(getString(R.string.whouser_reg_jobukid), jobPointUkid);
                fragment.setArguments(bundle);
                pushFragment(fragment);
            } else {

            }
        } else {
            toast(getString(R.string.warehouse_reg_cliclfast));
        }
    }

    private void showPickDateAndTime() {
        new DateAndTimePickerDialog.Builder(getActivity())
                .setData(null)
                .setTitle(getString(R.string.warehouse_reg_timepick_title))
                .setOnDateSelectedListener(new DateAndTimePickerDialog.OnDateTimeSelectedListener() {
                    @Override
                    public void onDateTimeSelected(String s) {
                        idWareRegTimeTv.setText(s);
                        contentMap.put(POSITION_DATE_TIME, s);
                    }

                    @Override
                    public void onCancel() {

                    }
                })
                .create()
                .show();
    }

    private void hideInput(View view) {
        InputMethodManager imm = (InputMethodManager)
                mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    private void showPickUnitDialog() {
        if (unitContent == null || unitContent.isEmpty()) {
            //没有数据
            requestData(REQUEST_UNIT_TWICE_CODE, TaskCenterConstant.GET_UNIT, getUnitMap);
            return;
        }
        new ChoosePickerDialog.Builder(mActivity)
                .setData(unitContent)
                .setTitle(getString(R.string.warehouse_reg_pick_bill))
                .setOnDataSelectedListener(new ChoosePickerDialog.OnSelectedListener() {
                    @Override
                    public void onSelected(String s, int i) {
                        idWareRegTypeTv.setText(s);
                        contentMap.put(POSITION_UNIT, billBeanList.get(i));
                    }

                    @Override
                    public void onCancel() {

                    }
                })
                .create()
                .show();
    }

    private void hidePop() {
        if (mEasyPopupWindow != null && mEasyPopupWindow.isShowing()) {
            mEasyPopupWindow.dismiss();
        }
    }

    /**
     * 核心method
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == GALLERY && resultCode == RESULT_OK) {
            //单选
            path = data.getStringExtra(KEY_SELECT_IMAGE_PATH);
        } else if (requestCode == REQUEST_UPLOAD_IMAGE_CAMERA && resultCode == RESULT_OK) {
            //拍照
            path = mFile.getAbsolutePath();
        }
        if (mEasyPopupWindow != null && mEasyPopupWindow.isShowing()) {
            mEasyPopupWindow.dismiss();
        }
        fileMap.clear();
        isComplete = false;
        if (POSITION_ONE == currentItem) {
            idWareRegImg1.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage("file://" + path, idWareRegImg1);
            if (binaryMap.containsKey(POSITION_ONE) && binaryMap.get(POSITION_ONE) != null) {
                binaryMap.get(POSITION_ONE).cancel();
            }
            removePosData(POSITION_ONE);
            fileMap.put(POSITION_ONE, path);
            uploadPhoto(fileMap, POSITION_ONE);
        } else if (POSITION_TWO == currentItem) {
            idWareRegImg2.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage("file://" + path, idWareRegImg2);
            if (binaryMap.containsKey(POSITION_TWO) && binaryMap.get(POSITION_TWO) != null) {
                binaryMap.get(POSITION_TWO).cancel();
            }
            removePosData(POSITION_TWO);
            fileMap.put(POSITION_TWO, path);
            uploadPhoto(fileMap, POSITION_TWO);
        } else if (POSITION_THREE == currentItem) {
            idWareRegImg3.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage("file://" + path, idWareRegImg3);
            if (binaryMap.containsKey(POSITION_THREE) && binaryMap.get(POSITION_THREE) != null) {
                binaryMap.get(POSITION_THREE).cancel();
            }
            removePosData(POSITION_THREE);
            fileMap.put(POSITION_THREE, path);
            uploadPhoto(fileMap, POSITION_THREE);
        }
    }

    /**
     * 图片上传逻辑
     *
     * @param fileMap
     * @param what
     */
    private void uploadPhoto(final Map<Integer, String> fileMap, int what) {
        if (!NetUtils.isHttpConnected(mActivity)) {
            toast(getString(R.string.warehouse_reg_no_network));
            return;
        }
        binaries = NoHttpUtils.httpUploadImgRequest(what, Constant.URL_UPLOADS_AVATAR, fileMap, new NoHttpUtils.OnFileUploadListener() {
            @Override
            public void onStart(int position) {
            }

            @Override
            public void onCancel(int position) {
                posIsActCancel.put(position, true);
                if (contentMap.containsKey(position) && contentMap.get(position) != null) {
                    contentMap.remove(position);
                    idWareRegNextTv.setClickable(false);
                    idWareRegNextTv.setBackground(getResources().getDrawable(R.drawable.task_center_nextbt_background));
                }

            }

            @Override
            public void onProgress(int position, int progress) {
                if (progress == PROGRESS_COMPLETE) {
                    fileMap.remove(position);
                    binaryMap.remove(position);
                }
                if (position == POSITION_ONE) {
                    idWarePro1.setVisibility(View.VISIBLE);
                    idWarePro1.setProgress(progress);
                } else if (position == POSITION_TWO) {
                    idWarePro2.setVisibility(View.VISIBLE);
                    idWarePro2.setProgress(progress);
                } else if (position == POSITION_THREE) {
                    idWarePro3.setVisibility(View.VISIBLE);
                    idWarePro3.setProgress(progress);
                }
            }

            @Override
            public void onFinish(int position) {
            }

            @Override
            public void onError(int position, Exception exception) {
            }
        }, new NoHttpUtils.OnFileUploadResultListener() {
            @Override
            public void onSucceed(int what, final FileUploadBean uploadBean) {
                if (what == POSITION_ONE) {
                    idWarePro1.setVisibility(View.INVISIBLE);
                } else if (what == POSITION_TWO) {
                    idWarePro2.setVisibility(View.INVISIBLE);
                } else if (what == POSITION_THREE) {
                    idWarePro3.setVisibility(View.INVISIBLE);
                }
                if(uploadBean == null || uploadBean.getData() == null || uploadBean.getData().isEmpty()){
                    toast("第" + what + "张图片上传失败");
                    contentMap.put(what, null);
                    return;
                }
                contentMap.put(what, uploadBean.getData().get(0));
            }

            @Override
            public void onFailed(int what, FileUploadBean uploadBean) {
                if (posIsActCancel.containsKey(what) && posIsActCancel.get(what) != null && posIsActCancel.get(what)) {
                    return;
                }
                if (what == POSITION_ONE) {
                    idWarePro1.setVisibility(View.INVISIBLE);
                    toast(getString(R.string.warehouse_reg_pos1img_uploadfailed));
                } else if (what == POSITION_TWO) {
                    idWarePro2.setVisibility(View.INVISIBLE);
                    toast(getString(R.string.warehouse_reg_pos2img_uploadfailed));
                } else if (what == POSITION_THREE) {
                    idWarePro3.setVisibility(View.INVISIBLE);
                    toast(getString(R.string.warehouse_reg_pos3img_uploadfailed));
                }

            }
        });
        if (binaries != null && binaries.size() > 0) {
            binaryMap.put(what, binaries.get(0));
        }
    }

    /**
     * 图片选择
     *
     * @param pos
     */
    private void showPop(int pos) {
        View popView = View.inflate(mActivity, R.layout.upload_image_popupwindow, null);
        popView.setTag(R.id.whouse_imgpic, pos);
        mEasyPopupWindow = new EasyPopupWindow.Builder(mActivity)
                //设置popupwindow view 里面可以传view, layout
                .setView(popView)
                //设置宽高
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                //设置背景透明度
                .setBackgroundAlpha(0.7f)
                //设置动画
                .setAnim(R.style.anim_menu_bottom_bar)
                //设置动画
                .setAnim(com.wwwarehouse.common.R.style.anim_menu_bottom_bar)
                //点击外面是否关闭
                .setOutsideTouchHide(true)
                //布局中的事件回调
                .setOnChildClickListener(new EasyPopupWindow.ChildClickListener() {
                    @Override
                    public void getChildView(int layoutId, View view) {
                        int item = (int) view.getTag(R.id.whouse_imgpic);
                        currentItem = item;
                        Button photoBt = (Button) view.findViewById(R.id.btn_take_photo);
                        Button galleryBt = (Button) view.findViewById(R.id.btn_gallery);
                        Button cancelBt = (Button) view.findViewById(R.id.btn_cancel);
                        photoBt.setOnClickListener(WhouseRegFirstFragment.this);
                        galleryBt.setOnClickListener(WhouseRegFirstFragment.this);
                        cancelBt.setOnClickListener(WhouseRegFirstFragment.this);
                    }
                })
                //创建
                .create();
        mEasyPopupWindow.showAtLocation(mActivity.findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }

    /**
     * 监视器
     */
    class ScanRunnable implements Runnable {

        @Override
        public void run() {
            while (!isReady) {
                if (contentMap.containsKey(POSITION_ONE) && contentMap.get(POSITION_ONE) != null &&
                        contentMap.containsKey(POSITION_TWO) && contentMap.get(POSITION_TWO) != null &&
                        contentMap.containsKey(POSITION_THREE) && contentMap.get(POSITION_THREE) != null &&
                        contentMap.containsKey(POSITION_DATE_TIME) && !StringUtils.isNullString((String)contentMap.get(POSITION_DATE_TIME)) &&
                        contentMap.containsKey(POSITION_UNIT) && contentMap.get(POSITION_UNIT) != null &&
                        contentMap.containsKey(POSITION_NUM) && !StringUtils.isNullString((String) contentMap.get(POSITION_NUM)) &&
                        !isComplete) {
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            idWareRegNextTv.setClickable(true);
                            idWareRegNextTv.setBackground(getResources().getDrawable(R.drawable.task_center_nextbt_background_blue));
                            isComplete = true;
                        }
                    });
                }
            }
        }
    }

    /**
     * 移除对应view的数据,并将按钮置灰
     *
     * @param pos
     */
    private void removePosData(int pos) {
        if (contentMap.containsKey(pos) && contentMap.get(pos) != null) {
            contentMap.remove(pos);
            idWareRegNextTv.setClickable(false);
            idWareRegNextTv.setBackground(getResources().getDrawable(R.drawable.task_center_nextbt_background));
        }
    }

    /**
     * 蓝牙结果回调
     *
     * @param event
     */
    public void onEventMainThread(BluetoothScanResultEvent event) {
        //蓝牙扫描结果
        if (event == null || StringUtils.isNullString(event.getMsg())) {
            return;
        }
        idWareRegEt.setText(event.getMsg());
        contentMap.put(POSITION_NUM, event.getMsg());
    }

    private String jobPointUkid;
    private String businessId;
    @Override
    public void requestDatas() {
        Bundle bundle = new Bundle();
        if(bundle != null){
            jobPointUkid = bundle.getString("jobPointUkid");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> permissionList) {
        //当requestCode相同时候，permissions.size == 请求的权限数时候，认为权限请求成功
        if (requestCode == 1 && permissionList.size() == 2) {
            Intent intent = new Intent();
            // 指定开启系统相机的Action
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            // 根据文件地址创建文件
            mFile = mFileUtils.getImagesFile("IMG_" + DateUtil.formatDate(System.currentTimeMillis(), "yyyyMMdd_HHmmss") + ".jpg");
            Uri uri;

            //判断是否是AndroidN以及更高的版本
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + BuildConfig.ProviderSuffix, mFile);
            } else {
                // 把文件地址转换成Uri格式
                uri = Uri.fromFile(mFile);
            }
            // 设置系统相机拍摄照片完成后图片文件的存放地址
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent, REQUEST_UPLOAD_IMAGE_CAMERA);
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> permissionList) {
        //调用了一次拒绝权限认为失败
        ToastUtils.showToast("拒绝了权限" + permissionList.get(0));
        PermissionUtils.permissionDialog(getActivity(), permissionList, null);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        isReady = true;
        if (scanRunnable != null) {
            scanRunnable = null;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        contentMap.put(POSITION_NUM, s.toString());
    }

}
